﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData;
using dhDataAcess.produceLinq;
using baseSet.importExport;
using dhDataAcess.baseBLL;

namespace dhsystem.produceForm.pdrk
{
    public partial class FrmpdckDetailAdd : Form
    {
        pdrkBLL pdrk = new pdrkBLL();
        pdrkDetailModel pdrkDetailRow = new pdrkDetailModel();
        productListBLL product = new productListBLL();
        BindingSource BindingSourcepdrkDetailAdd;
        productUnitBLL productunit = new productUnitBLL();
        string state;
        string billNo;
        public FrmpdckDetailAdd(BindingSource bindingSourcepdrkDetail, string _state, string _billNo)
        {
            InitializeComponent();
            BindingSourcepdrkDetailAdd = bindingSourcepdrkDetail;
            state = _state;
            billNo = _billNo;
        }

        private void FrmpdckDetailAdd_Load(object sender, EventArgs e)
        {
            if (state == "编辑")
            {

                textbillNo.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "billNo");
                textpname.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "pName");
                textqty.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "unit");
                textmark.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "mark");
                //textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                // BindingSourceDetailAdd.AddNew();
                //textproductname.Text = productname;
            }
            if (state == "新增")
            {
                textbillNo.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "billNo");
                textpname.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "pName");
                textqty.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "unit");
                textmark.DataBindings.Add("text", BindingSourcepdrkDetailAdd, "mark");
                // textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                BindingSourcepdrkDetailAdd.AddNew();
                textbillNo.Text = billNo;
                textunit.Text = "Kg";
            }

            textpname.Focus();
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    basicSql.getproductawightUpdate(textpname.Text, textpwight.Text);
                    BindingSourcepdrkDetailAdd.EndEdit(); //编辑到完成，写到数据源
                    BindingSourcepdrkDetailAdd.CancelEdit(); //刷新数据作用

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textqty_TextChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    if (textpwight.Text != "0")
            //    {

            //        string L = (Convert.ToDecimal(textqty.Text) / Convert.ToDecimal(textpwight.Text)).ToString("#0.00");
            //        textL.Text = L;
            //        textM.Text = (Convert.ToDouble(L) * Convert.ToDouble(basicSql.getUnitrateM("GR"))).ToString("#0.00");

            //        textY.Text = (Convert.ToDouble(L) * Convert.ToDouble(basicSql.getUnitrateY("GR"))).ToString("#0.00");
            //        textkg.Text = textqty.Text;
            //    }
            //}
            //catch (FormatException)
            //{
            //    MessageBox.Show("输入正确的数量");
            //}
        }

        private void textpname_TextChanged(object sender, EventArgs e)
        {
            textpwight.Text = basicSql.getgrwightbyproductList(textpname.Text);
            if (basicSql.getstockQty(textpname.Text) == "")
            {
                textstockqty.Text = "0";
            }
            else
            {
                textstockqty.Text = basicSql.getstockQty(textpname.Text);
            }
        }

        private void FrmpdckDetailAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            BindingSourcepdrkDetailAdd.CancelEdit();
        }

        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "pname";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.productList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textpname.Text = productselect.pname;
            }
        }

        private void textqty_MouseLeave(object sender, EventArgs e)
        {
            try
            {
                if (textpwight.Text == "0")
                {
                    textpwight.Text = "1";
                }
                else 
                {

                    string L = (Convert.ToDecimal(textqty.Text) / Convert.ToDecimal(textpwight.Text)).ToString("#0.00");
                    textL.Text = L;
                    textM.Text = (Convert.ToDouble(L) * Convert.ToDouble(basicSql.getUnitrateM("GR"))).ToString("#0.00");

                    textY.Text = (Convert.ToDouble(L) * Convert.ToDouble(basicSql.getUnitrateY("GR"))).ToString("#0.00");
                    textkg.Text = textqty.Text;
                }
            }
            catch (FormatException)
            {
                MessageBox.Show("输入正确的数量");
            }
        }
    }
}
